package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.yxx.business.entity.*;
import com.yxx.common.annotation.RepeatSubmit;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.service.ITbEquipmentConfigService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 设备配置Controller
 *
 * @author yxx
 * @date 2025-08-05
 */
@Tag(name = "设备配置管理-TbEquipmentConfig")
@RestController
@RequestMapping("/business/equipmentConfig")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbEquipmentConfigController extends BaseControllerPlus {

    private final ITbEquipmentConfigService tbEquipmentConfigService;

    @Operation(summary = "查询--获取阶段碳排放数据")
    @GetMapping(value = "/phase")
    public R<TbEquipmentPhaseVo> getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return R.ok(tbEquipmentConfigService.getPhase(tbEquipmentPhaseDto));
    }

    @Operation(summary = "查询--获取所有阶段碳排放数据")
    @GetMapping(value = "/phase/all")
    public R<List<TbEquipmentPhaseVo>> getPhaseAll(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return R.ok(tbEquipmentConfigService.getPhaseAll(tbEquipmentPhaseDto));
    }

    @Operation(summary = "新增--新增阶段碳排放数据")
    @PostMapping(value = "/phase")
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<String> savePhase(@RequestBody @Validated TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return toResult(tbEquipmentConfigService.savePhase(tbEquipmentPhaseVo));
    }

    @Operation(summary = "修改--修改阶段碳排放数据")
    @PostMapping(value = "/phase/{id}")
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<String> updatePhase(@PathVariable Long id, @RequestBody @Validated TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return toResult(tbEquipmentConfigService.updatePhase(id, tbEquipmentPhaseVo));
    }

    @Operation(summary = "查询--设备配置列表")
    @GetMapping("/list")
    public PageResult<TbEquipmentConfigVo> list(@ParameterObject TbEquipmentConfigQueryDto tbEquipmentConfigQueryDto) {
        return tbEquipmentConfigService.selectTbEquipmentConfigVoPage(tbEquipmentConfigQueryDto);
    }

    @Operation(summary = "查询--设备配置单个")
    @GetMapping(value = "/{equipmentId}")
    public R<TbEquipmentConfigVo> getInfo(@PathVariable Long equipmentId) {
        return R.ok(tbEquipmentConfigService.selectTbEquipmentConfigVoByEquipmentId(equipmentId));
    }

    @Operation(summary = "新增--设备配置")
    @PreAuthorize("@ss.hasPermi('business:equipmentConfig:add')")
    @Log(title = "设备配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbEquipmentConfigEditDto tbEquipmentConfigQueryDto) {
        return toResult(tbEquipmentConfigService.insertTbEquipmentConfig(tbEquipmentConfigQueryDto));
    }

    @Operation(summary = "修改--设备配置")
    @PreAuthorize("@ss.hasPermi('business:equipmentConfig:edit')")
    @Log(title = "设备配置", businessType = BusinessType.UPDATE)
    @PutMapping("/{equipmentId}")
    public R<String> update(@PathVariable Long equipmentId, @RequestBody @Validated TbEquipmentConfigEditDto tbEquipmentConfigQueryDto) {
        return toResult(tbEquipmentConfigService.updateTbEquipmentConfig(equipmentId, tbEquipmentConfigQueryDto));
    }

    @Operation(summary = "删除--设备配置")
    @PreAuthorize("@ss.hasPermi('business:equipmentConfig:remove')")
    @Log(title = "设备配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentIds}")
    public R<String> delete(@PathVariable List<Long> equipmentIds) {
        return toResult(tbEquipmentConfigService.deleteTbEquipmentConfigByEquipmentIds(equipmentIds));
    }

    @Operation(summary = "导出--设备配置列表")
    @PreAuthorize("@ss.hasPermi('business:equipmentConfig:export')")
    @Log(title = "设备配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbEquipmentConfigQueryDto tbEquipmentConfigQueryDto) {
        List<TbEquipmentConfigDo> list = tbEquipmentConfigService.selectTbEquipmentConfigDoList(tbEquipmentConfigQueryDto);
        ExcelUtil<TbEquipmentConfigDo> util = new ExcelUtil<>(TbEquipmentConfigDo.class);
        util.exportExcel(response, list, "设备配置数据");
    }

    @Operation(summary = "导出--设备配置模板")
    @PreAuthorize("@ss.hasPermi('business:equipmentConfig:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbEquipmentConfigDo> util = new ExcelUtil<>(TbEquipmentConfigDo.class);
        util.importTemplateExcel(response, "导入设备配置模板数据");
    }

    @Operation(summary = "导入--设备配置列表")
    @PreAuthorize("@ss.hasPermi('business:equipmentConfig:import')")
    @Log(title = "设备配置", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbEquipmentConfigDo> util = new ExcelUtil<>(TbEquipmentConfigDo.class);
        List<TbEquipmentConfigDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbEquipmentConfigService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
